package com.sita.bike.ui.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;

import com.sita.bike.R;
import com.sita.bike.ui.activity.DataAnalysisActivity;
import com.sita.bike.ui.adapter.MonthGatherAdapter;

import java.util.LinkedList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.support.v7.widget.RecyclerView.OnScrollListener;


public class MonthGatherFragment extends Fragment {
    public static MonthGatherFragment instance;
    @Bind(R.id.recyclerHorizontalView)
    public RecyclerView recyclerView;
    LinearLayout item;
    private int recyclerViewWidth;

    public static MonthGatherFragment getInstance() {
        instance = new MonthGatherFragment();
        return instance;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_month_gather, container, false);
        ButterKnife.bind(this, view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);


        final List<String> dataset = new LinkedList<>();
        //月份其实是添加了18个，前3个和后3个是
        for (int t = 0; t < 18; t++) {
            if (t < 3 || t > 14) {
                dataset.add("");
            } else {
                dataset.add("" + (t - 2));
            }
        }

        //获取recyclerView 的宽度
        ViewTreeObserver vto = recyclerView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                recyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                recyclerViewWidth = recyclerView.getWidth();
                final MonthGatherAdapter adapter = new MonthGatherAdapter(dataset, recyclerViewWidth);
                adapter.setOnItemClickListener(new MonthGatherAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        RecyclerView.LayoutManager mLayoutManager = recyclerView.getLayoutManager();
                        ((LinearLayoutManager) mLayoutManager).scrollToPositionWithOffset(position - 3, 0);
                        DataAnalysisActivity.getInstance().setCurrentMonth(Integer.valueOf(position - 3));
                        adapter.setSelectItem(position);
                        adapter.notifyDataSetChanged();

                    }
                });

                recyclerView.setAdapter(adapter);
                //获得现在的月份，然后移动到该月份
                int month = DataAnalysisActivity.getSystemMonth();//从零开始
                recyclerView.scrollToPosition(month);
                adapter.setSelectItem(month + 3);
                adapter.notifyDataSetChanged();
            }
        });
//        scrollChange();
        return view;
    }

    public void scrollChange() {
        int screenWidth = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        final int avg = (screenWidth + 36) / 22;
        recyclerView.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dx <= avg) {
                    if (dx <= (avg / 2)) {
                        recyclerView.scrollTo(-(dx), dy);
                    } else {
                        recyclerView.scrollTo(avg, dy);
                    }
                } else if ((dx % avg) <= (avg / 2)) {
                    recyclerView.scrollTo((dx - (dx % avg)), dy);
                } else if ((dx % avg) > (avg / 2)) {
                    recyclerView.scrollTo((dx - (dx % avg) + avg), dy);
                }
            }
        });
    }
}
